﻿namespace dotnet_notes.Extensions
{
    /// <summary>
    /// 文件扩展类
    /// </summary>
    public static class FileExtension
    {
        /// <summary>
        /// 保存文件到指定路径
        /// </summary>
        /// <param name="formFile"></param>
        /// <param name="filePath">文件完整路径</param>
        /// <returns></returns>
        public static async Task SaveToAsync(this IFormFile formFile, string filePath)
        {
            using var fs = new FileStream(filePath, FileMode.Create);
            await formFile.CopyToAsync(fs);
        }

        /// <summary>
        /// 将文件转换成字节数据
        /// </summary>
        /// <param name="formFile">文件</param>
        /// <returns></returns>
        public static async Task<byte[]> ToArrayAsync(this IFormFile formFile)
        {
            using var ms = new MemoryStream();
            await formFile.CopyToAsync(ms);

            return ms.ToArray();
        }
    }
}
